﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Citiport.Util.Language;

/// <summary>
/// Class for cookis management
/// Author: Jeff
/// Date: 04/04/2008
/// </summary>
public class CookiesManager
{
    private HttpResponse _response;
    private HttpRequest _request;


    /// <summary>
    /// Constructor
    /// </summary>
    /// <param name="userRequest">HttpRequest</param>
    /// <param name="serverResponse">HttpRespsonse</param>
	public CookiesManager(HttpRequest userRequest, HttpResponse serverResponse)
	{
        _request = userRequest;
        _response = serverResponse;
        this.initContext();
	}

    /// <summary>
    /// default constructor
    /// </summary>
    public CookiesManager()
    {
        this.initContext();
    }

    private void initContext()
    {
        if (_request == null || _response == null)
        {
            HttpContext current = HttpContext.Current;
            _request = current.Request;
            _response = current.Response;
        }
    }

    /// <summary>
    /// Add and edit cookie
    /// </summary>
    /// <param name="key">key</param>
    /// <param name="value">value</param>
    /// <param name="cookieExpire">cookie expire date</param>
    public void addCookie(String key, String value, DateTime cookieExpire)
    {
        HttpCookie cookie = _request.Cookies[key];
        if ( cookie == null)
        {
            cookie = new HttpCookie(key);
            
        }
        cookie.Value = value;
        cookie.Expires = cookieExpire;
        _response.Cookies.Set(cookie);
    }

    /// <summary>
    /// Set user id in cookie
    /// EX: han = 1
    /// </summary>
    /// <param name="userid"></param>
    public void setCookieUserId(String userid)
    {
        addCookie(Citiport.Core.Const.COOKIE_USER_ID, userid, DateTime.Now.AddYears(30));
    }

    /// <summary>
    /// Set user preferred language in cookie
    /// </summary>
    /// <param name="lang_no">language no. Notice: in Int EX: 1</param>
    public void setCookieUserPrefLang(int lang_no)
    {
        addCookie(Citiport.Core.Const.COOKIE_USER_LANG, lang_no.ToString().Trim(), DateTime.Now.AddYears(30));
    }

    /// <summary>
    /// Set user preferred language in cookie
    /// </summary>
    /// <param name="lang_no">language code. Notice: in String EX: en</param>
    public void setCookieUserPrefLangCode(String lang_code)
    {
        addCookie(Citiport.Core.Const.COOKIE_USER_LANG, lang_code.ToString().Trim(), DateTime.Now.AddYears(30));
    }

    /// <summary>
    /// Set user accessible languages in cookie
    /// </summary>
    /// <param name="lang_nos">language nos. Notice string EX: 1,15,16</param>
    public void setCookieUserAccLang(String lang_nos)
    {
        addCookie(Citiport.Core.Const.COOKIE_USER_ACCESSIBLE_LANG, lang_nos.Trim(), DateTime.Now.AddYears(30));
    }

    /// <summary>
    /// Get cookie value
    /// </summary>
    /// <param name="key">key</param>
    /// <returns>value</returns>
    public String getCookieValue(String key)
    {
        if (isCookiesSupported())
        {
            if (_request.Cookies[key] == null)
                return null;
            String value = _request.Cookies[key].Value;
            return value;
        }
        else
            return null;//TODO:throws excepion

    }

    /// <summary>
    /// Is Browser supported cookie
    /// </summary>
    /// <returns>True, whether user browser supports cookie</returns>
    public bool isCookiesSupported()
    {
        return _request.Browser.Cookies;
    }

    /// <summary>
    /// Remove cookie
    /// </summary>
    /// <param name="key">key</param>
    public void removeCookie(String key)
    {
        if (_response.Cookies[key] != null)
        {
            _response.Cookies[key].Value = null;
            _response.Cookies[key].Expires = DateTime.Now.AddDays(-1);//TODO:Change to 1900/1/1
        }
    }
}
